<?php

/**

 * @see Alinks_ImportExport_Import_Interface

 */

require_once('Alinks/ImportExport/Import/Interface.php');



/**

 * Abstract class for import classes

 *

 * Copyright (C) 2008  Sean Hickey

 * 

 * This program is free software: you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *

 * @author Sean Hickey <sean@headzoo.com>

 * @version $Id: Abstract.php 391 2008-07-03 05:42:56Z sean $

 * @package Alinks_ImportExport

 */

abstract class Alinks_ImportExport_Import_Abstract implements Alinks_ImportExport_Import_Interface

{

	/**

	 * Whether to import settings from the export file

	 */

	const IMPORT_SETTINGS = 'settings';

	

	/**

	 * Whether to import factory settings from the export file

	 */

	const IMPORT_FACTORY_SETTINGS = 'factory_settings';

	

	/**

	 * Array of import options

	 *

	 * @var array

	 */

	protected $_options = array();

	

	/**

	 * Constructor

	 *

	 * @param array $options Import options

	 */

	public function __construct(array $options = array())

	{

		$this->setOptions($options);

	}

	

	/**

	 * Sets the import options

	 *

	 * @param array $options The import options

	 * @return Alinks_ImportExport_Import_Abstract

	 */

	public function setOptions(array $options)

	{

		$this->_resetOptions();

		foreach($options as $key => $value) {

			$this->addOption($key, $value);

		}

		

		return $this;

	}

	

	/**

	 * Adds a single import option

	 *

	 * @param string $key The option key

	 * @param mixed $value The option value

	 * @return Alinks_ImportExport_Import_Abstract

	 */

	public function addOption($key, $value)

	{

		$this->_options[$key] = $value;

		return $this;

	}

	

	/**

	 * Returns the import options

	 *

	 * If $key is specified, then only the option value for that

	 * key will be returned. Otherwise the full array of options

	 * will be returned.

	 *

	 * @param string $key The option key

	 * @return mixed

	 * @throws Alinks_ImportExport_Import_Exception

	 */

	public function getOptions($key = null)

	{

		if (null !== $key && !isset($this->_options[$key])) {

			/**

			 * @see Alinks_ImportExport_Import_Exception

			 */

			require_once('Alinks/ImportExport/Import/Exception.php');

			throw new Alinks_ImportExport_Import_Exception("The option value for $key key is not set");

		}

		

		if (null !== $key) {

			return $this->_options[$key];

		}

		

		return $this->_options;

	}

	

	/**

	 * Sets the keyphrases found in the import file

	 *

	 * @param Alinks_Keyphrase[] $keyphrases The keyphrases

	 * @return bool

	 */

	protected function _setKeyphrases(array $keyphrases)

	{

		$updated = false;

		foreach($keyphrases as $keyphrase) {

			if (Alinks_Registry::get('Alinks_Keyphrase_Keeper')->update($keyphrase)) {

				$updated = true;

			}

		}

		

		if ($updated) {

			/**

			 * @see Alinks_Post_Parser

			 */

			require_once('Alinks/Post/Parser.php');

			$cacher = Alinks_Registry::get('cacher');

			if ($cacher) {

				$cacher->flush(Alinks_Post_Parser::CACHE_NS_COMMENTS);

				$cacher->flush(Alinks_Post_Parser::CACHE_NS_POSTS);

			}

		}

		

		return true;

	}

	

	/**

	 * Sets the settings found in an import file

	 *

	 * @param array $settings The settings

	 * @return bool

	 */

	protected function _setSettings(array $settings)

	{

		foreach($settings as $key => $value) {

			Alinks_Settings::set('alinks', $key, $value);

		}

		

		return true;

	}

	

	/**

	 * Sets the factory settings found in the import file

	 *

	 * @param array $settings The factory settings

	 * @return bool

	 */

	protected function _setFactorySettings(array $settings)

	{

		foreach($settings as $factory => $factory_settings) {

			foreach($factory_settings as $key => $value) {

				Alinks_Settings::set($factory, $key, $value);

			}

		}

		

		return true;

	}

	

	/**

	 * Sets the options to their default values

	 */

	protected function _resetOptions()

	{

		$this->_options = array();

		$this->_options[self::IMPORT_SETTINGS] = false;

		$this->_options[self::IMPORT_FACTORY_SETTINGS] = false;

	}

}

